﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.UserManager;
using SMS.ServiceManager;

namespace SMS.Admin.Modules
{
    public partial class DetailCode : WebControlBase
    {
        private int codeId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!ClientState.IsAuthenticated)
            {
                Response.Redirect("~/Login.aspx?session=timeout");
            }

            codeId = Request.QueryString["codeId"] != null ? Int32.Parse(Request.QueryString["codeId"].ToString().Trim()) : -1;

            if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1)
            {
                if (codeId == -1)
                {
                    lnkAdd.Enabled = false;
                    lnkAdd.Enabled = false;
                }
            }

            if (IsPostBack) return;

            BindInfo();
        }

        protected void lnkAdd_Click(object sender, System.EventArgs e)
        {
            if (Page.IsValid)
            {
                DoSave();
            }
        }

        protected void lnkBack_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("Default.aspx?module=ListCode");
        }

        private void BindInfo()
        {
            CodeInfo objCode = CodeManager.Select(codeId);

            if (objCode != null)
            {
                this.txtCodeName.Text = objCode.CodeName;
                this.ddlType.SelectedValue = objCode.CodeType.ToString();
                this.txtCodeValue.Text = objCode.CodeValue;
                this.txtContent.Text = objCode.Contents;
                this.txtInfo.Text = objCode.Info;
            }
        }

        private bool DoSave()
        {
            try
            {
                bool result = false;
                if (codeId > 0)
                {
                    CodeInfo objCode = CodeManager.Select(codeId);
                    if (objCode != null)
                    {
                        objCode.CodeID = codeId;
                        objCode.CodeName = this.txtCodeName.Text;
                        objCode.CodeType = this.ddlType.SelectedValue;
                        objCode.CodeValue = this.txtCodeValue.Text;
                        objCode.Contents = this.txtContent.Text;
                        objCode.Info = this.txtInfo.Text;

                        result = CodeManager.Update(objCode);
                        if (result)
                            lblError.Text = "Cập nhật thành công!";
                        else
                            lblError.Text = "Cập nhật lỗi";
                    }
                }
                else
                {
                    CodeInfo objCode = new CodeInfo();
                    objCode.CodeName = this.txtCodeName.Text;
                    objCode.CodeType = this.ddlType.SelectedValue;
                    objCode.CodeValue = this.txtCodeValue.Text;
                    objCode.Contents = this.txtContent.Text;
                    objCode.Info = this.txtInfo.Text;

                    result = CodeManager.Insert(objCode);
                    if (result)
                        lblError.Text = "Cập nhật thành công!";
                    else
                        lblError.Text = "Cập nhật lỗi";
                }
                return result;
            }
            catch
            {
                return false;
            }
        }
    }
}
